﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.Windows.Media.Imaging;
using System.ServiceModel;
using TsSilverlight.ServiceReferenceMatch;


namespace TsSilverlight
{
    public partial class Media : Page
    {
        private int videoIndexPlaying = 0;
        private WCFMatch selectedMatch = null;

        public Media()
        {
            InitializeComponent();
                                   
            LoadSports();
          
        }

        private void buttonPlay_Click(object sender, RoutedEventArgs e)
        {
            this.StartNextVideo();
        }

        private void buttonPause_Click(object sender, RoutedEventArgs e)
        {
            mediaElementVideo.Pause();
        }

        private void buttonStop_Click(object sender, RoutedEventArgs e)
        {
            mediaElementVideo.Stop();
        }

        private void mediaElementVideo_MediaEnded(object sender, RoutedEventArgs e)
        {
            this.StartNextVideo();
        }

        //Custom Methods

        private void StartNextVideo()
        {
            List<Uri> uriList = this.GetVideoList();
            mediaElementVideo.Stop();
            mediaElementVideo.Position = new TimeSpan(0);

            if (uriList.Count > 0)
            {
                if (this.videoIndexPlaying == uriList.Count)
                {
                    this.videoIndexPlaying = 0;
                }
                
                mediaElementVideo.Source = uriList.ElementAt(this.videoIndexPlaying);
                mediaElementVideo.Play();

                this.videoIndexPlaying++;
            }
            else
            {
                mediaElementVideo.Stop();
                mediaElementVideo.Position = new TimeSpan(0);
                mediaElementVideo.Source = null;
            }
        }

        private void AddImagesToPage()
        {
            this.wrapPanelImages.Children.Clear();

            foreach (Uri auxUri in this.GetImageList())
            {
                Image newImage = new Image();
                newImage.Height = 135;
                newImage.Width = 180;
                newImage.Stretch = Stretch.Fill;
                newImage.Source = new BitmapImage(auxUri);

                Border newBorder = new Border();
                newBorder.CornerRadius = new CornerRadius(20);
                newBorder.BorderThickness = new Thickness(4);
                newBorder.Child = newImage;

                this.wrapPanelImages.Children.Add(newBorder);
            }
        }

        private List<Uri> GetVideoList()
        {
            List<Uri> uriList = new List<Uri>();
            string[] VideoExtensions = new string[] { ".flv", ".asf", ".avi", ".wmv", ".mp4", ".mov", ".3gp", ".m4v", ".3g2" };
            if (selectedMatch != null)
            {
                foreach (string s in selectedMatch.MediaList)
                {
                    string ext = s.Substring(s.Length - 4);
                    if (VideoExtensions.Contains(ext))
                    {
                        string url = HostWebSite + s.Remove(0, 2);
                        uriList.Add(new Uri(url));
                    }

                }
            }
            return uriList;
        }

        private List<Uri> GetImageList()
        {
            List<Uri> uriList = new List<Uri>();
            string[] ImageExtensions = new string[] { ".gif", ".jpeg", ".jpg", ".png", ".bmp" };

            if (selectedMatch != null)
            {
                foreach (string s in selectedMatch.MediaList)
                {
                    string ext = s.Substring(s.Length - 4).ToLower();
                    if (ImageExtensions.Contains(ext))
                    {
                        string url = HostWebSite + s.Remove(0, 2);
                        uriList.Add(new Uri(url));
                    }
                }
            }
            return uriList;
        }

        private string HostWebSite
        {
            get
            {
                string host = App.Current.Host.Source.AbsoluteUri;
                int clientBin = host.IndexOf("ClientBin", 0);
                if (clientBin == -1)
                    return "Could not find \"ClientBin\" in " + host;

                return host.Substring(0, clientBin);
            }
        }
        
        private void LoadSports()
        {
            ServiceMatchClient client = new ServiceMatchClient();
            client.GetSportListCompleted += new EventHandler<GetSportListCompletedEventArgs>(client_GetSportListCompleted);       
            client.GetSportListAsync();
        }

        void client_GetSportListCompleted(object sender, GetSportListCompletedEventArgs e)
        {
            comboBoxSports.DisplayMemberPath = "Name";
            comboBoxSports.ItemsSource = e.Result;
            if (comboBoxSports.Items.Count > 0)
            {
                comboBoxSports.SelectedIndex = 0;
                comboBoxSports.UpdateLayout();
                LoadMatches();
            }
        }
        
        private void Login()
        {
           
          
        }

        private void LoadMatches()
        {
            ServiceMatchClient client = new ServiceMatchClient();
            client.GetMatchListCompleted += new EventHandler<GetMatchListCompletedEventArgs>(client_GetMatchListCompleted);
            client.GetMatchListAsync(((WCFSport)this.comboBoxSports.SelectedItem));
        }

        void client_GetMatchListCompleted(object sender, GetMatchListCompletedEventArgs e)
        {
            comboBoxMatch.DisplayMemberPath = "Name";
            comboBoxMatch.ItemsSource = e.Result;
            if (comboBoxMatch.Items.Count > 0)
            {
                comboBoxMatch.SelectedIndex = 0;                
                comboBoxMatch.UpdateLayout();
                selectedMatch = (WCFMatch)comboBoxMatch.SelectedItem;
            }
        
        }

        private void comboBoxSports_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.selectedMatch = null;
            LoadMatches();
            this.videoIndexPlaying = 0;
            AddImagesToPage();
            StartNextVideo(); 
        }

        private void comboBoxMatch_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            selectedMatch = (WCFMatch)comboBoxMatch.SelectedItem;
            if (selectedMatch != null)
            {
                this.videoIndexPlaying = 0;
                AddImagesToPage();
                StartNextVideo();                
            }
        }
    }
}
